package main

import (
	"github.com/veandco/go-sdl2/sdl"
)

type Sprite struct {
	W int32
	H int32
	X int32
	Y int32
	t *Texture
}

func NewSprite(x, y, w, h int32, t *Texture) *Sprite {
	return &Sprite{
		W: w,
		H: h,
		X: x,
		Y: y,
		t: t,
	}
}

func (this *Sprite) Render(src *sdl.Rect, dst *sdl.Rect) {
	this.t.Render(src, dst)
}

func (this *Sprite) SetWH(w, h int32) {
	this.W = w
	this.H = h
}

func (this *Sprite) GetWH() (int32, int32) {
	return this.W, this.H
}

func (this *Sprite) SetXY(x, y int32) {
	this.X = x
	this.Y = y
}

func (this *Sprite) GetXY() (int32, int32) {
	return this.X, this.Y
}

func (this *Sprite) GetRect() *sdl.Rect {
	return &sdl.Rect{
		X: this.X,
		Y: this.Y,
		W: this.W,
		H: this.H,
	}
}

type PlayerSprite struct {
	*Sprite
	rects   [4][4][2]*sdl.Rect
	rectIdx int
	frame   int
	lv      int
	direct  int
}

func NewPlayer1Sprite(t *Texture) *PlayerSprite {
	p := &PlayerSprite{
		Sprite: NewSprite(0, 0, 40, 40, t),
	}
	p.rects[0][0][0] = &sdl.Rect{643, 3, 26, 26}
	p.rects[0][0][1] = &sdl.Rect{643, 35, 26, 26}
	p.rects[0][1][0] = &sdl.Rect{675, 3, 26, 26}
	p.rects[0][1][1] = &sdl.Rect{675, 35, 26, 26}
	p.rects[0][2][0] = &sdl.Rect{707, 3, 26, 26}
	p.rects[0][2][1] = &sdl.Rect{707, 35, 26, 26}
	p.rects[0][3][0] = &sdl.Rect{739, 3, 26, 26}
	p.rects[0][3][1] = &sdl.Rect{739, 35, 26, 26}

	/*
		p.rects[3][0][0] = &sdl.Rect{643, 3, 26, 26}
		p.rects[3][0][1] = &sdl.Rect{643, 35, 26, 26}
		p.rects[3][1][0] = &sdl.Rect{675, 3, 26, 26}
		p.rects[3][1][1] = &sdl.Rect{675, 35, 26, 26}
		p.rects[3][2][0] = &sdl.Rect{707, 3, 26, 26}
		p.rects[3][2][1] = &sdl.Rect{707, 35, 26, 26}
		p.rects[3][3][0] = &sdl.Rect{739, 3, 26, 26}
		p.rects[3][3][1] = &sdl.Rect{739, 35, 26, 26}
	*/

	return p
}

func NewPlayer2Sprite(t *Texture) *PlayerSprite {
	p := &PlayerSprite{
		Sprite: NewSprite(200, 200, 40, 40, t),
	}
	p.rects[0][0][0] = &sdl.Rect{643, 3, 26, 26}
	p.rects[0][0][1] = &sdl.Rect{643, 35, 26, 26}
	p.rects[0][1][0] = &sdl.Rect{675, 3, 26, 26}
	p.rects[0][1][1] = &sdl.Rect{675, 35, 26, 26}
	p.rects[0][2][0] = &sdl.Rect{707, 3, 26, 26}
	p.rects[0][2][1] = &sdl.Rect{707, 35, 26, 26}
	p.rects[0][3][0] = &sdl.Rect{739, 3, 26, 26}
	p.rects[0][3][1] = &sdl.Rect{739, 35, 26, 26}

	/*
		p.rects[3][0][0] = &sdl.Rect{643, 3, 26, 26}
		p.rects[3][0][1] = &sdl.Rect{643, 35, 26, 26}
		p.rects[3][1][0] = &sdl.Rect{675, 3, 26, 26}
		p.rects[3][1][1] = &sdl.Rect{675, 35, 26, 26}
		p.rects[3][2][0] = &sdl.Rect{707, 3, 26, 26}
		p.rects[3][2][1] = &sdl.Rect{707, 35, 26, 26}
		p.rects[3][3][0] = &sdl.Rect{739, 3, 26, 26}
		p.rects[3][3][1] = &sdl.Rect{739, 35, 26, 26}
	*/

	return p
}

func (this *PlayerSprite) SetDirect(d int) {
	this.direct = d
}

func (this *PlayerSprite) GetDirect() int {
	return this.direct
}

func (this *PlayerSprite) Render() {
	if this.frame < 10 {
		this.frame += 1
	} else {
		this.frame = 0
		this.rectIdx += 1
		if this.rectIdx == len(this.rects[this.lv][this.direct]) {
			this.rectIdx = 0
		}
	}
	dst := &sdl.Rect{X: this.X, Y: this.Y, W: this.W, H: this.H}
	this.Sprite.Render(this.rects[this.lv][this.direct][this.rectIdx], dst)
}

func CheckCollision(a, b *sdl.Rect, lead sdl.Rect) bool {
	leftA := a.X + lead.X
	rightA := a.X + lead.X + a.W
	topA := a.Y + lead.Y
	bottomA := a.Y + lead.Y + a.H

	leftB := b.X
	rightB := b.X + b.W
	topB := b.Y
	bottomB := b.Y + b.H

	if bottomA < topB {
		return false
	}
	if topA > bottomB {
		return false
	}
	if rightA < leftB {
		return false
	}
	if leftA > rightB {
		return false
	}
	return true
}
